package com.hanbang.maige.mode.javabean.base;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.google.gson.annotations.SerializedName;
import com.hanbang.maige.R;
import com.hanbang.maige.code.ActivityManager;
import com.hanbang.maige.code.broadcast.BroadcastUtils;
import com.hanbang.maige.utils.db.LiteOrmUtil;
import com.hanbang.maige.view.login.activity.LoginActivity;
import com.litesuits.orm.db.annotation.Column;
import com.litesuits.orm.db.annotation.Ignore;
import com.litesuits.orm.db.annotation.PrimaryKey;
import com.litesuits.orm.db.annotation.Table;
import com.litesuits.orm.db.assit.QueryBuilder;
import com.litesuits.orm.db.assit.WhereBuilder;
import com.litesuits.orm.db.enums.AssignType;
import com.litesuits.orm.db.model.ColumnsValue;
import com.litesuits.orm.db.model.ConflictAlgorithm;


/**
 * 用户个人信息
 */
@Table("UserData")
public class UserData {
    //用户对象
    @Ignore
    public static UserData userData;

    @PrimaryKey(AssignType.BY_MYSELF)
    @Column("id")
    @SerializedName("UserId")
    private int id;
    //是否是当前登录的用户（这样就不用sb保存用户ID）
    @Column("isLogin")
    private boolean isLogin;
    /**
     * UserId : 3
     * TelPhone : 18662288251
     * NiChenName : mg622882
     * Sex : 1
     * Age : 1
     * HeadPhoto :
     * HeadPhotoMid :
     * HeadPhotoSma :
     * Email :
     * BiaoQian : 9
     * TelphoneSystem : int
     * rongyuntoken :
     * yaoqingma :
     * cjsj : /Date(1477449455057)/
     * xgsj :
     */

    private String TelPhone;
    private String NiChenName;
    private int Sex;
    private int Age;
    private String HeadPhoto;
    private String HeadPhotoMid;
    private String HeadPhotoSma;
    private String Email;
    private String SuccessID;
    private int BiaoQian;
    private String TelphoneSystem;
    private String rongyuntoken;
    private String yaoqingma;
    private String cjsj;
    private String xgsj;
    /**
     * hyValueName : 计算机软件
     */

    private String hyValueName;


    public static UserData getDbUserData() {
        try {
            userData = LiteOrmUtil.getLiteOrm().query(QueryBuilder.create(UserData.class).where("isLogin=?", true).limit(0, 1)).get(0);
            return userData;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 直接判断是否登录
     *
     * @return
     */
    public static boolean isSLogin() {
        return getUserData() == null ? false : getUserData().isLogin;
    }

    /**
     * 退出登录
     * 吧数据库标识改成false 标记全部没有登录状态
     */
    public static boolean exitLogin(Context context) {
        //清除其他登录的用户
        int res = LiteOrmUtil.getLiteOrm().update(WhereBuilder.create(UserData.class).where("isLogin=?", true), new ColumnsValue(new String[]{"isLogin"}, new Boolean[]{false}), ConflictAlgorithm.None);
        userData = null;
        if (res <= 0) {
            return false;
        }
        ActivityManager.getInstance().exitAllActivity();
        /**
         * 发送退出广播
         */
        Intent intent = new Intent();
        intent.setAction(BroadcastUtils.EXIT_LOGIN);
        context.sendBroadcast(intent);
//            // 返回登录页面
        Intent intent2 = new Intent(context, LoginActivity.class);
        intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent2);
        return true;
    }

    /**
     * 退出登录,对话框
     */
    public void exit(final Context context) {

        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .title("提示")
                .content("确定退出当前账户吗？")
                .positiveText("残忍退出")
                .negativeText("继续使用")
                .negativeColorRes(R.color.gray_text)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        exitLogin(context);
                    }
                })
                .build();

        dialog.show();
    }

    public boolean setDbUserData() {
        return setDbUserData(this);
    }

    /**
     * 保存数据到数据库（）
     *
     * @param userData 被保存的数据，这个数据会被默认成数据库的唯一登录数据
     * @return
     */
    public static boolean setDbUserData(UserData userData) {
        if (userData != null) {
            try {
                //清除其他登录的用户
                LiteOrmUtil.getLiteOrm().update(WhereBuilder.create(UserData.class).where("isLogin=?", true), new ColumnsValue(new String[]{"isLogin"}, new Boolean[]{false}), ConflictAlgorithm.None);
                //设置数据库当前登录的用户
                userData.setLogin(true);
                LiteOrmUtil.getLiteOrm().save(userData);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        } else {
            return false;
        }
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public boolean isLogin() {
        return isLogin;
    }


    public void setLogin(boolean login) {
        isLogin = login;
    }


    public static UserData getUserData() {
        return userData == null ? getDbUserData() : userData;
    }


    public String getTelPhone() {
        return TelPhone;
    }

    public void setTelPhone(String TelPhone) {
        this.TelPhone = TelPhone;
    }

    public String getNiChenName() {
        return NiChenName;
    }

    public void setNiChenName(String NiChenName) {
        this.NiChenName = NiChenName;
    }

    public int getSex() {
        return Sex;
    }

    public void setSex(int Sex) {
        this.Sex = Sex;
    }

    public int getAge() {
        return Age;
    }

    public void setAge(int Age) {
        this.Age = Age;
    }

    public String getHeadPhoto() {
        return HeadPhoto;
    }

    public void setHeadPhoto(String HeadPhoto) {
        this.HeadPhoto = HeadPhoto;
    }

    public String getHeadPhotoMid() {
        return HeadPhotoMid;
    }

    public void setHeadPhotoMid(String HeadPhotoMid) {
        this.HeadPhotoMid = HeadPhotoMid;
    }

    public String getHeadPhotoSma() {
        return HeadPhotoSma;
    }

    public void setHeadPhotoSma(String HeadPhotoSma) {
        this.HeadPhotoSma = HeadPhotoSma;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String Email) {
        this.Email = Email;
    }

    public int getBiaoQian() {
        return BiaoQian;
    }

    public void setBiaoQian(int BiaoQian) {
        this.BiaoQian = BiaoQian;
    }

    public String getTelphoneSystem() {
        return TelphoneSystem;
    }

    public void setTelphoneSystem(String TelphoneSystem) {
        this.TelphoneSystem = TelphoneSystem;
    }

    public String getRongyuntoken() {
        return rongyuntoken;
    }

    public void setRongyuntoken(String rongyuntoken) {
        this.rongyuntoken = rongyuntoken;
    }

    public String getYaoqingma() {
        return yaoqingma;
    }

    public void setYaoqingma(String yaoqingma) {
        this.yaoqingma = yaoqingma;
    }

    public String getCjsj() {
        return cjsj;
    }

    public void setCjsj(String cjsj) {
        this.cjsj = cjsj;
    }

    public String getXgsj() {
        return xgsj;
    }

    public void setXgsj(String xgsj) {
        this.xgsj = xgsj;
    }

    public String getSuccessID() {
        return SuccessID;
    }

    public void setSuccessID(String successID) {
        SuccessID = successID;
    }

    public String getHyValueName() {
        return hyValueName;
    }
    public void setHyValueName(String hyValueName) {
        this.hyValueName = hyValueName;
    }
}
